package com.itouch.bracelet.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.itouch.bracelet.R;
import com.itouch.bracelet.adapter.PSportAdapter;
import com.itouch.bracelet.bean.PSport;
import com.itouch.bracelet.bean.PSportResult;
import com.itouch.bracelet.cache.UserCache;
import com.itouch.bracelet.diseaseinfo.Constants;
import com.itouch.widget.XListView;
import com.itouch.widget.XListView.IXListViewListener;

public class SportsListActivity extends BaseActivity implements IXListViewListener{

	private  XListView listview;
	private ArrayList<PSport> list;
	private PSportAdapter adapter;
	private int page_index =1;
	private static final int page_size = 10;

	
	@Override
	protected int setContentViewById() {
		return R.layout.activity_sport_list;
	}

	
	@Override
	protected void initViews() {
		listview = (XListView) findViewById(R.id.listview);
		listview.setPullRefreshEnable(true);
		listview.setPullLoadEnable(false);
		listview.setXListViewListener(this);
		listview.setRefreshTime(getTime());
//		listview.setOnItemClickListener(this);
		list = new ArrayList<PSport>();
		adapter = new PSportAdapter(context, list);
		listview.setAdapter(adapter);
		
		
		getList();
	}

	
	
	
	/**
	 * 获取城市的接口，获取后缓存
	 */
	private void getList() {
		Uri.Builder builder = Uri.parse(Constants.GET_SPORT)
				.buildUpon();
		//  http://192.168.3.46:81/handchain/mobi/sports/list?filter_S_customerId=86
		builder.appendQueryParameter("filter_S_customerId",  UserCache.getOauth(context).getUserId());
		builder.appendQueryParameter("page", page_index+"");
		builder.appendQueryParameter("rows",  page_size+"");
	
		if(page_index==1){
			list.clear();
		}
		
		
		show();
		getJSONByVolleyGET(builder.toString(), 
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						disMiss();onLoad();
						Log.d("GET_SPORT",
								"GET_SPORT" + arg0.toString());
						try {
							PSportResult a = new Gson().fromJson(
									arg0.toString(), PSportResult.class);
							if (a == null) {
								return;
							}
							if (a.isFlag()) {
									 List<PSport> xx = a.getRows();
									if (xx != null) {
										list.addAll(xx);
										adapter.notifyDataSetChanged();
										if(xx.size()<page_size){
											setPullLoadEnable(false);
											
										}else{
											setPullLoadEnable(true);
											
										}
								}

							} else {
								Toast.makeText(context,
										a.getMsg() + "",
										Toast.LENGTH_LONG).show();
							}
						} catch (Exception e) {
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						disMiss();onLoad();
						Toast.makeText(context,
								getString(R.string.net_error) + "",
								Toast.LENGTH_LONG).show();
					}
				});

	}
	
	
	@Override
	public void onRefresh() {
		page_index = 1;
		getList();
	}

	@Override
	public void onLoadMore() {
		page_index++;
		getList();
	}

	
	private void onLoad() {
		if (null != listview) {
			listview.stopRefresh();
			listview.stopLoadMore();
			listview.setRefreshTime(getTime());
		}
	}
	
	private void 	setPullLoadEnable(boolean temp){
		if(listview!=null){
			listview.setPullLoadEnable(temp);
		}
	}
	

	@Override
	protected void initData() {

	}

	
}
